/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:SearchAction.java
 * Version:1.0
 * Date:2012-1-13
 * Description: 查询Action
 * author:zhijian.zhang
 */
package com.tietiao.action.search;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tietiao.common.action.BaseAction;
import com.tietiao.dao.model.CommodityInfo;
import com.tietiao.dao.model.SysUser;
import com.tietiao.dao.model.UserCommidityFavorite;
import com.tietiao.service.commodity.CommodityService;
import com.tietiao.service.sysuser.SysUserService;
import com.tietiao.service.tietiao.CommidityFavoriteService;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class SearchAction extends BaseAction {
	@Resource
	private CommodityService commodityService;
	@Resource
	private CommidityFavoriteService commidityFavoriteService;
	@Resource
	private SysUserService sysUserService;
	// 页号
	private Integer pageNo;
	// 页大小
	private Integer pageSize;
	// 查找的内容
	private String searchTitle;
	// 查找的类型
	private Integer searchType;

	private String order;
	// 列表长度
	private Integer listLength;

	private List<UserCommidityFavorite> userCommidityFavorites;

	private List<SysUser> sysUsers;

	public List<SysUser> getSysUsers() {
		return sysUsers;
	}

	public void setSysUsers(List<SysUser> sysUsers) {
		this.sysUsers = sysUsers;
	}

	public List<UserCommidityFavorite> getUserCommidityFavorites() {
		return userCommidityFavorites;
	}

	public void setUserCommidityFavorites(
			List<UserCommidityFavorite> userCommidityFavorites) {
		this.userCommidityFavorites = userCommidityFavorites;
	}

	public Integer getListLength() {
		return listLength;
	}

	public void setListLength(Integer listLength) {
		this.listLength = listLength;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	private List<CommodityInfo> commodityInfos;

	public List<CommodityInfo> getCommodityInfos() {
		return commodityInfos;
	}

	public void setCommodityInfos(List<CommodityInfo> commodityInfos) {
		this.commodityInfos = commodityInfos;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public String getSearchTitle() {
		return searchTitle;
	}

	public void setSearchTitle(String searchTitle) {
		this.searchTitle = searchTitle;
	}

	public Integer getSearchType() {
		return searchType;
	}

	public void setSearchType(Integer searchType) {
		this.searchType = searchType;
	}

	// 进入查询主页
	public String searchIndex() {
		return dispatcher("/jsp/fg/search/search_index.jsp");
	}

	// 查询主方法
	public String search() {
		// 查询商品
		if (searchType == 0) {
			return searchCommodity();
		}
		// 查询用户
		if (searchType == 1) {
			return searchUser();
		}
		// 查询标签
		if (searchType == 2) {
			return searchFav();
		}
		return null;
	}

	// 查询商品信息
	private String searchCommodity() {
		commodityInfos = commodityService.searchCommodityInfos(searchTitle,
				getUserId(), (pageNo - 1) * pageSize, pageSize, order);
		listLength = commodityInfos.size();
		return dispatcher("/jsp/fg/search/search_loadCommodityInfos.jsp");
	}

	// 查询收藏夹
	private String searchFav() {
		userCommidityFavorites = commidityFavoriteService.searchFavsByFavName(
				searchTitle, getUserId(), (pageNo - 1) * pageSize, pageSize,
				order);
		listLength = userCommidityFavorites.size();
		return dispatcher("/jsp/fg/search/search_loadFav.jsp");
	}

	// 查寻用户
	private String searchUser() {
		sysUsers = sysUserService.searchSysUsersByPetName(searchTitle,
				getUserId(), (pageNo - 1) * pageSize, pageSize, order);
		listLength = sysUsers.size();
		return dispatcher("/jsp/fg/search/search_loadUser.jsp");
	}

	/**
	 * 
	 * 功能：获取用户ID信息 用户未登陆时返回null
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2012-1-14
	 * 
	 * @return
	 */
	private Integer getUserId() {
		if (getUser() != null) {
			return getUser().getUserId();
		}
		return null;
	}

}
